<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="流程定义ID" prop="processDefinitionId">
        <el-input
          v-model="queryParams.processDefinitionId"
          placeholder="请输入流程定义ID"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程实例ID" prop="processInstanceId">
        <el-input
          v-model="queryParams.processInstanceId"
          placeholder="请输入流程实例ID"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="流程定义名称" prop="modelNprocessDefinitionNameame">
        <el-input
          v-model="queryParams.processDefinitionName"
          placeholder="请输入流程定义名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="流程定义KEY" prop="processDefinitionKey">
        <el-input
          v-model="queryParams.processDefinitionKey"
          placeholder="请输入流程定义KEY"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
          <el-checkbox v-model="queryParams.unfinished">未完成</el-checkbox>
          <el-checkbox v-model="queryParams.finished">已完成</el-checkbox>
      </el-form-item>
      <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-table v-loading="loading" :data="instanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程实例ID" prop="id" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="流程定义KEY" prop="processDefinitionKey" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="流程定义名称" prop="processDefinitionName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="版本号" prop="version" align="center">
        <template slot-scope="scope"><el-tag>V{{ scope.row.processDefinitionVersion }}</el-tag></template>
      </el-table-column>
      <el-table-column label="开始时间" prop="startTime" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="结束时间" prop="endTime" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="操作" align="center">
         <template slot-scope="{row}">
          <el-dropdown>
              <span class="el-dropdown-link">操作<i class="el-icon-arrow-down el-icon--right"></i></span>
               <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item
                        icon="el-icon-edit" 
                        @click.native="btnShow(row)">查看
                      </el-dropdown-item>
                   
                      <el-dropdown-item icon="el-icon-edit" @click.native="btnView(row.id)" divided >查看详情
                      </el-dropdown-item>
                      <el-dropdown-item icon="el-icon-delete" divided @click.native="btnDelete(row.id,false)">删除
                      </el-dropdown-item>
                      <el-dropdown-item icon="el-icon-delete" divided @click.native="btnDelete(row.id,true)">删除包含历史
                      </el-dropdown-item>
                       
               </el-dropdown-menu>
          </el-dropdown>
         </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  <process-detail v-if="dialogViewVisible" :visible.sync="dialogViewVisible"
                        :processInstanceId="processInstanceId"></process-detail>
  <el-dialog title="流程实例" :visible.sync="dialogFormVisible">
    <el-form ref="dataForm" :model="temp" disabled
              label-position="right" label-width="110px">
        <el-row>
            <el-col :span="12">
                <el-form-item label="流程实例ID" prop="id">
                    <el-input v-model="temp.id"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="业务主键Key" prop="businessKey">
                    <el-input v-model="temp.businessKey"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="流程定义ID" prop="processDefinitionId">
                    <el-input v-model="temp.processDefinitionId"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="流程定义名称" prop="processDefinitionName">
                    <el-input v-model="temp.processDefinitionName"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="流程定义KEY" prop="processDefinitionKey">
                    <el-input v-model="temp.processDefinitionKey"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="流程定义版本" prop="processDefinitionVersion">
                    <el-input v-model="temp.processDefinitionVersion"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker v-model="temp.startTime" style="width: 200px" type="datetime"
                                    value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker v-model="temp.endTime" style="width: 200px" type="datetime"
                                    value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="启动人ID" prop="startUserId">
                    <el-input v-model="temp.startUserId"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="启动人姓名" prop="startUserName">
                    <el-input v-model="temp.startUserName"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="启动节点ID" prop="startActivityId">
                    <el-input v-model="temp.startActivityId"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="耗时(毫秒)" prop="durationInMillis">
                    <el-input v-model="temp.durationInMillis"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="子流程ID" prop="superProcessInstanceId">
                    <el-input v-model="temp.superProcessInstanceId"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">

                <el-form-item label="状态" prop="suspended">
                    <el-input v-if="temp.endTime==null" v-model="temp.suspended?'挂起':'激活'"/>
                    <el-input v-else value="结束"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="删除原因" prop="deleteReason">
                    <el-input v-model="temp.deleteReason"/>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
           
  </el-dialog>
  </div>
</template>
<script>
    import {Message} from 'element-ui'
import { listInstance,queryById,deleteInstance } from "@/api/flowable/instance";
import ProcessDetail from "../components/ProcessDetail";
export default {
  name: "process",
   components:{
    ProcessDetail
  },
  data() {
    return {
      temp: {
          id: undefined,
          businessKey: '',
          processDefinitionId: '',
          processDefinitionName: '',
          processDefinitionKey: '',
          processDefinitionVersion: '',
          startTime: undefined,
          endTime: undefined,
          durationInMillis: undefined,
          startUserId: '',
          startUserName: '',
          startActivityId: '',
          superProcessInstanceId: '',
          suspended: undefined,
          deleteReason: ''
      },
      dialogFormVisible: false,
      dialogViewVisible:false,
      processInstanceId:undefined,
      selectedRecords: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      instanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processDefinitionId: undefined,
        processInstanceId: undefined,
        processDefinitionName: undefined,
        processDefinitionKey:undefined,
        unfinished:false,
        finished:false
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listInstance(this.queryParams).then(
        response => {
          this.instanceList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
     /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },


    btnView(processInstanceId) {
        this.processInstanceId = processInstanceId
        this.dialogViewVisible = true
    },
    btnShow(row) {
       
        queryById( {processInstanceId: row.id}).then(res => {
            const {data} = res
            this.temp = Object.assign({}, data)
            this.dialogFormVisible = true
        })
    },
    btnDelete(id, cascade) {
        if (!id) {
            Message.error('请选择要删除的记录')
            return
        }
        deleteInstance(id,cascade).then(({msg}) => {
            Message.success(msg)
            this.getList();
        })
        
    }
   
    
     
  }
};
</script>